<?php
ini_set('register_globals', 'On');
ini_set('memory_limit', '1024M');
ini_set('session.bug_compat_42','Off');
ini_set('session.bug_compat_warn','Off');
//error_reporting(0);
session_start();
header('content-type:text/html;charset=utf-8');
define('ADMIN', TRUE);
require_once 'include/inc.php';
require_once 'conf/conf.php';
require_once 'include/functions.php';
if (!isset($_SERVER['PHP_AUTH_USER'])||!isset($_SERVER['PHP_AUTH_PW'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    exit;
}else{
    $user=$_SERVER['PHP_AUTH_USER'];
    $pwd=$_SERVER['PHP_AUTH_PW'];
//    echo $conf['users'][$user],$pwd;
    if(isset($conf['users'][$user])&&$conf['users'][$user]!==$pwd)
    {
     header('WWW-Authenticate: Basic realm="My Realm"');
     header('HTTP/1.0 401 Unauthorized');
     exit;
    }
}
$act = G('act') ? G('act') : 'index';
global $redis;
if('index'!=$act)
{
 //  echo $conf['redis'][0]['ip'];
 $r=$conf['redis'][0];
 redis_init($r['ip'],$r['port']);
}
$key=G('key');
$type=G('type');

$_SESSION['key']=isset($key)?$key:$_SESSION['key'];
$_SESSION['type']=isset($type)?$type:$_SESSION['type'];
$type=$_SESSION['type'];
switch ($act) {
    case 'index':
        require_once 'template/index.phtml';
        break;
    case 'query':
        is_ajax();
        $key=G('key');
        $type=G('type');
        fixRedisConnection();
        if(G('key')&&isset($type)){
            switch($type){
                case 0:
                    exit(json_encode(redis_list(G('key'))));
                    break;
                case 1:
                    exit(json_encode(redis_get_key(G('key'))));
                    break;
                case 2:
                    $arr=redis_get_hash(G('key'));
                    exit(json_encode($arr));
                    break;
                case 3:
                    exit(json_encode(redis_get_list(G('key'))));
                    break;
                case 4:
                    exit(json_encode(redis_get_set(G('key'))));
                    break;
                case 5:
                    exit(json_encode(redis_get_zset(G('key'))));
                    break;
                default:
                    break;
            }
        }
        break;
    case 'test':
//        $redis->flushAll();
        fixRedisConnection();
        foreach(range(0,200) as $k=>$v)
        {
         // redis_set_key($v, md5(rand(0,$v)));
//         $redis->lSet('LIST',$v,md5(rand(0,$v)));
           $redis->sAdd('SET',md5(rand(0,$v)));
//         $redis->hSet('HASH',$v,md5(rand(0,$v)));
//         $redis->lPush('LIST',md5(rand(0,$v)));
//         $redis->zAdd('ZSET',$v,md5(rand(0,$v)));
        }
        break;
    case 'info':
        is_ajax();
        fixRedisConnection();
//        pr(redis_info());
        $arr=redis_info();
        exit(json_encode($arr));
        break;
    case 'delete':
        is_ajax();
        fixRedisConnection();
        $key=G('key');
        $type=G('type');
        $value=G('value');
        $index=G('index');
        $hkey=G('hkey');
        if(isset($key)&&isset($type))
        {
          $ret=redis_delete($key,$type,$index,$value,$hkey);
          if($ret)
          {
              $retArr=array('code'=>'0000','msg'=>'<span style="background-color:gray">删除成功</span>');
              exit(json_encode($retArr));
          }else{
               $retArr=array('code'=>'0001','msg'=>'<spans tyle="background-color:red">删除失败</span>');
              exit(json_encode($retArr));
          }

        }
        break;
    case 'save':
        is_ajax();
        fixRedisConnection();
        $key=G('key');
        $type=G('type');
        $value=G('value');
        $ttl=G('ttl');
        if(isset($key)&&isset($type)&&isset($value)&&isset($ttl))
        {
          $ret=redis_do_update($key, $value, $type, $ttl);
          $retArr=array();
          if($ret)
          {
            $retArr['code']='0000';
            $retArr['msg']='修改成功';
          }else{
            $retArr['code']='0001';
            $retArr['msg']='修改失败';
          }
          exit(json_encode($retArr));
        }
        break;
    case 'list':
        is_ajax();
        fixRedisConnection();
        $key=G('key');
        if(isset($key)){
          $str=json_encode(redis_list($key));
          exit($str);
        }
        break;
    case 'update':
        $key=G('key');
        $type=G('type');
        $value=G('value');
        $index=G('index');
        $hkey=G('hkey');
        $oldvalue=G('oldvalue');
        if(isset($key)&&isset($type))
        {
          $ret=redis_do_key_update($key,$type,$index,$value,$oldvalue,$hkey);
          $retArr=array();
          if($ret)
          {
            $retArr['code']='0000';
            $retArr['msg']='修改成功';
          }else{
            $retArr['code']='0001';
            $retArr['msg']='修改失败';
          }
          exit(json_encode($retArr));
        }
        break;
    case 'add':
        is_ajax();
        fixRedisConnection();
        $key=G('key');
        $type=G('type');
        $value=G('value');
        $index=G('index');
        $hkey=G('hkey');
        $scope=G('scope');
        if(isset($key)&&isset($type))
        {
            $ret=redis_do_add($key,$type,$index,$value,$hkey);
               $retArr=array();
              if($ret)
              {
                $retArr['code']='0000';
                $retArr['msg']='添加成功';
              }else{
                $retArr['code']='0001';
                $retArr['msg']='添加失败';
              }
              exit(json_encode($retArr));
        }
        break;
    default:
        header('location:index.php?act=index');
        break;
}